package com.xiao.tripmateai.controller;

import com.xiao.tripmateai.common.BaseResponse;
import com.xiao.tripmateai.common.ResultUtils;
import com.xiao.tripmateai.exception.ErrorCode;
import com.xiao.tripmateai.exception.ThrowUtils;
import com.xiao.tripmateai.model.domain.Conversation;
import com.xiao.tripmateai.model.dto.chat.UserChatCursorPageRequest;
import com.xiao.tripmateai.model.dto.chat.UserChatCursorPageResponse;
import com.xiao.tripmateai.model.dto.chat.UserChatHistoryRequest;
import com.xiao.tripmateai.service.SpringAiChatMemoryService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/chat")
public class ChatController {

    @Resource
    private SpringAiChatMemoryService springAiChatMemoryService;

    /**
     * 根据用户id获取用户的对话记录
     * @param request
     * @return
     */
    @PostMapping("/history")
    public BaseResponse<List<Conversation>> getChatHistory(
            @RequestBody UserChatHistoryRequest request
    ) {
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        List<Conversation> conversations = springAiChatMemoryService.getUserConversations(request);
        return ResultUtils.success(conversations);
    }

    /**
     * 游标查询历史对话记录
     * @param request
     * @return
     */
    @PostMapping("/history/cursor")
    public BaseResponse<UserChatCursorPageResponse> getChatHistoryByCursor(
            @Valid @RequestBody UserChatCursorPageRequest request
    ) {
        UserChatCursorPageResponse response = springAiChatMemoryService.getUserConversationsByCursor(request);
        return ResultUtils.success(response);
    }
}
